<template>
  <div class="new-wrap">
    <!-- <div class="login-wrap">
      <el-container class="kpi-login is-vertical">
        <div class="main flex-1">
          <div class="login-form flex flex-column">
            <h1 class="title">{{ title }}</h1>
            <div class="subtitle">数字证书登录</div>
            <div class="kpi-icon flex-1"></div>
            <div class="btn-small">请插入数字证书</div>

            <div
              class="login-btn"
              v-if="loading"
              v-loading="loading"
              element-loading-background="rgba(0, 0, 0, 0)"
            ></div>
            <div class="login-btn" v-else @click="submit">登录</div>
          </div>
        </div>
        <div class="footer">
          {{ footerCopyRightText }}
          <span class="admin-link" @click="$emit('change')">login</span>
        </div>
      </el-container>

      <div style="display:none;">
        <div>
          <form id="certAuth" name="certAuth" method="post" action="jitGWAuth">
            <input id="authMode" name="authMode" type="hidden" value="cert" />

            <input
              type="hidden"
              id="original"
              name="original"
              :value="originalData.original"
            />

            <input type="hidden" id="signed_data" name="signed_data" />
          </form>
        </div>
      </div>
    </div> -->
    <!-- <div class="login-warp-bg"></div> -->
    <div class="new-content">
      <div class="new-box">
        <div class="new-left"></div>
        <div class="new-right">
          <div class="new-from" style="display:block">
            <div
              style="position: absolute;
    top: 48px;
    left: 100px"
            >
              <div
                class="btn-small"
                style="padding-left:40px;font-size:14px;color:#999999"
              >
                请插入数字证书
              </div>
              <div class="kpi-icon flex-1" style="margin-top:10px"></div>
            </div>
          </div>
          <div
            class="login-btn"
            v-if="loading"
            v-loading="loading"
            element-loading-background="#fff"
          ></div>
          <div
            class="login-btn new-btn-bg"
            v-if="!loading"
            @click="submit"
          ></div>
        </div>
      </div>
      <div class="new-abs">
        <div class="new-line1">
          <img src="../../assets/images/logo-yuan.png" alt="" />
        </div>
        <div class="new-line2">欢迎使用</div>
        <div class="new-line3">
          <img src="../../assets/images/logo-cs.png" alt="" />
        </div>
        <div class="new-line4">{{ title }}</div>
      </div>
    </div>

    <div style="display:none;">
      <div>
        <form id="certAuth" name="certAuth" method="post" action="jitGWAuth">
          <input id="authMode" name="authMode" type="hidden" value="cert" />

          <input
            type="hidden"
            id="original"
            name="original"
            :value="originalData.original"
          />

          <input type="hidden" id="signed_data" name="signed_data" />
        </form>
      </div>
    </div>
    <div class="footer new-foot">
      {{ footerCopyRightText }}
      <span class="admin-link" @click="$emit('change')">login</span>
    </div>
  </div>
</template>

<script>
import { generateRandom, caAuth } from "@/api/api";
import LoginService from "./service";

const INIT_PARAM =
  '<?xml version="1.0" encoding="utf-8"?><authinfo><liblist><lib type="PM" version="1.0" dllname="Q3J5cHRPY3guZGxs"><algid val="SHA1" sm2_hashalg="SM3" /></lib><lib type="CSP" version="1.0" dllname="TWljcm9zb2Z0IEVuaGFuY2VkIENyeXB0b2dyYXBoaWMgUHJvdmlkZXIgdjEuMA=="><algid val="SHA1" sm2_hashalg="SHA1" /></lib><lib type="CSP" version="1.0" dllname="TWljcm9zb2Z0IFN0cm9uZyBDcnlwdG9ncmFwaGljIFByb3ZpZGVy"><algid val="SHA1" sm2_hashalg="SHA1" /></lib><lib type="CSP" version="1.0" dllname="RW50ZXJTYWZlIGVQYXNzMzAwMyBDU1AgdjEuMA=="><algid val="SHA1" sm2_hashalg="SHA1" /></lib><lib type="CSP" version="1.0" dllname="SklUIFVTQiBLZXkgQ1NQIHYxLjA="><algid val="SHA1" sm2_hashalg="SHA1" /></lib><lib type="CSP" version="1.0" dllname="RW50ZXJTYWZlIGVQYXNzMjAwMSBDU1AgdjEuMA=="><algid val="SHA1" sm2_hashalg="SHA1" /></lib><lib type="CSP" version="1.0" dllname="SklUIFVTQiBLZXkzMDAzIENTUCB2MS4w"><algid val="SHA1" sm2_hashalg="SHA1" /></lib><lib type="CSP" version="1.0" dllname="TWljcm9zb2Z0IEJhc2UgQ3J5cHRvZ3JhcGhpYyBQcm92aWRlciB2MS4w"><algid val="SHA1" sm2_hashalg="SHA1" /></lib><lib type="CSP" version="1.0" dllname="RkVJVElBTiBlUGFzc05HIFJTQSBDcnlwdG9ncmFwaGljIFNlcnZpY2UgUHJvdmlkZXI="><algid val="SHA1" sm2_hashalg="SHA1" /></lib><lib type="CSP" version="1.0" dllname="RkVJVElBTiBlUGFzc05HIENTUCBGb3IgSklUM0sgVjEuMA=="><algid val="SHA1" sm2_hashalg="SHA1" /></lib><lib type="SKF" version="1.1" dllname="U2h1dHRsZUNzcDExXzMwMDBHTS5kbGw="><algid val="SHA1" sm2_hashalg="SM3" /></lib><lib type="CSP" version="3.0" dllname="ZVNhZmUgQ3J5cHRvZ3JhcGhpYyBTZXJ2aWNlIFByb3ZpZGVyIHYxLjA="><algid val="SHA1" sm2_hashalg="SHA1" /></lib></liblist></authinfo>';

export default {
  data: function() {
    return {
      loading: false,
      initParam: INIT_PARAM,
      originalData: {}
    };
  },
  computed: {
    title() {
      return window.pageData.login.loginTitle;
    },
    PNXClientPath() {
      return `${location.origin}/PNXClient.exe`;
    },
    footerCopyRightText() {
      return window.pageData.login.footerCopyRightText;
    }
  },
  mounted() {
    setTimeout(() => {
      this.init();
    }, 200);
  },
  methods: {
    init() {
      window.original = "";
      // 如果未安装插件，则提示安装。
      try {
        JIT_GW_ExtInterface.GetVersion();
        //ca认证开始
        this.caAuthRun();
      } catch (e) {
        console.log(e);
        window.location.href = this.PNXClientPath;
      }
    },
    submit() {
      this.init();
    },
    async caAuthRun() {
      try {
        this.loading = true;
        await this._generateRandom();
        await this._caAuth();
      } catch (error) {
        this.$message.error("连接失败");
      } finally {
        this.loading = false;
      }
    },
    async _generateRandom() {
      try {
        const result = await generateRandom();
        this._checkSuccess(result);
        this.originalData = result.data.data;
        window.original = this.originalData.original;
      } catch (error) {
        throw error;
      }
    },
    async _caAuth() {
      try {
        var dataProcess = doDataProcess(this.initParam);
        if (!dataProcess) {
          throw "验证u盘失败";
        }

        var params = {
          authMode: "cert",
          originalData: this.originalData.original_data,
          originalJsp: this.originalData.original,
          signedData: dataProcess
        };

        const result = await caAuth(params);

        if (!result.data.success) {
          throw "用户登陆失败";
        }

        LoginService.saveUser(result.data.data);
        this.$router.push({ path: "/aiSearch" });
      } catch (error) {
        throw error;
      }
    },
    _checkSuccess(result) {
      if (result.data.resultCode !== 0) {
        throw new Error();
      }
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.new-wrap {
  display: flex;
  justify-content: center;
  align-items: Center;
  height: 100%;
  background: #f8f9fb;
}
.new-fix {
  position: fixed;
  right: 0;
  bottom: 0;
  width: 200px;
  height: 200px;
}
.new-foot {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  text-align: center;
  font-weight: 400;
  color: rgba(100, 99, 99, 1);
  font-size: 12px;
  line-height: 36px;
}
.new-box {
  .new-btn-bg {
    background: url("../../assets/images/logo-btn.png") no-repeat center right;
    background-size: 92px 42px;
  }
  width: 960px;
  margin: 0 auto;
  display: flex;
  margin-top: 320px;
  position: relative;
  .new-left {
    background: #1e212c;
    width: 480px;
    height: 200px;
  }
  .new-right {
    background: #fff;
    width: 480px;
    height: 200px;
    display: flex;
    position: relative;
    .login-btn {
      cursor: pointer;
      width: 80px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      // background: #164de0;
      color: #fff;
      border-radius: 5px;
      position: absolute;
      top: 85px;
      right: 50px;
    }
    .new-from {
      padding-left: 0px;
      width: 350px;
      display: flex;
      align-items: center;
      .el-form {
        width: 90%;
      }
      .kpi-icon {
        background: url("../../assets/images/kpi-icon.png") no-repeat center;
        background-size: 158px 68px;
        height: 68px;
        width: 158px;
        /* margin: 100px auto 100px -10px */
      }
    }
    // .login-btn {
    //   width: 92px;
    //   height: 42px;
    //   background: url("../../assets/images/logo-btn.png") no-repeat center right;
    //   background-size: 92px 42px;
    //   position: absolute;
    //   bottom: 58px;
    //   right: 20px;
    // }
  }
}
/deep/ .el-input__inner {
  border: none;
  height: 36px;
  line-height: 36px;
  border-bottom: 1px solid #c9cdd0;
}
.new-abs {
  position: absolute;
  width: auto;
  top: 278px;
  left: 170px;
  img {
    display: block;
    width: 100%;
  }
  .new-line1 {
    width: 70px;
    height: 70px;
    margin-bottom: 30px;
  }
  .new-line2 {
    color: #e0e0e0;
    font-size: 26px;
    font-weight: 400;
    margin-bottom: 10px;
  }
  .new-line3 {
    width: 76px;
    // height: 19px;
    margin-bottom: 10px;
  }
  .new-line4 {
    color: #e0e0e0;
    font-size: 20px;
    font-weight: 800;
  }
}
.new-content {
  position: relative;
  width: 1200px;
  height: 500px;
  background: url("../../assets/images/logo-bg.png") no-repeat center right;
  background-size: 1200px 440px;
}

.login-wrap {
  position: fixed;
  height: 100%;
  width: 100%;
  z-index: 2;
}
.login-warp-bg {
  position: fixed;
  height: 100%;
  width: 100%;
  z-index: 1;
  background-image: linear-gradient(to right, #095cfe 27%, #0ebcfb 98%);
}
.admin-link {
  color: transparent;
  float: right;
  display: inline-block;
  height: 50px;
  width: 100px;
  cursor: pointer;
}
.kpi-login {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  /* min-height: 800px; */
  min-width: 800px;
}

.kpi-login .main {
  /* background: transparent!important; */
  position: relative;
  top: 0;
  left: 0;
  width: 100%;
  height: calc(100% - 66px);
  min-height: 400px;
  background: url("../../assets/images/back-ground1.png") no-repeat center right;
  background-size: auto;
}

/* .card-pf {
    position: absolute;
    padding: 0 !important;
    left: 0;
    right: 0;
    top: 0;
    margin: 0;
    width: 100%;
    height: 100%;
    background-color: transparent !important;
} */

.login-form {
  position: absolute;
  top: 55%;
  width: 300px;
  left: 0px;
  text-align: center;
  -webkit-transform: translate(0, -55%);
  transform: translate(0, -55%);
  height: 62%;
}

.login-form .title {
  font-size: 38px;
  font-weight: 600;
  color: #ffffff;
  height: 46px;
  margin: 0;
}

.login-form .subtitle {
  color: #ffffff;
  font-size: 22px;
  font-weight: 300;
  margin-top: 10px;
}

.login-form .btn-small {
  color: #ffffff;
  font-size: 18px;
  font-weight: 300;
}

.login-form .kpi-icon {
  background: url("../../assets/images/kpi-icon.png") no-repeat center;
  background-size: 288px 156px;
  background-size: 15.5vw 13vh;
  height: 156px;
  min-height: 80px;
  /* margin: 100px auto 100px -10px */
}

.login-form .login-btn {
  background-color: #018aff;
  color: #fff;
  font-size: 20px;
  height: 50px;
  line-height: 50px;
  border-radius: 30px;
  width: 340px;
  margin: 10px auto 0 auto;
  cursor: pointer;
}

.kpi-login .footer {
  background-color: rgba(102, 102, 102, 1);
  width: 100%;
  height: 66px;
  line-height: 66px;
  color: #ffffff;
  font-size: 14px;
  text-align: center;
  z-index: 2;
}
</style>
